home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 4
/
Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso
/
Graphics
/
Utilities
/
CFG 2.1 UR
/
Convert Earlier Than 1.2
/
convert file.c
next >
Wrap
C/C++ Source or Header
|
1990-10-08
|
11KB
|
458 lines
#include <stdio.h>
#include <math.h>
#include "OSUtil.h"
#include "StdFilePkg.h"
#include "FileMgr.h"
typedef struct JS_FRACTAL_120
{
float version;
int function;
long accuracy;
int freedom;
double centerX;
double centerY;
double scale;
double fixedRe;
double fixedIm;
Boolean invertKolor;
Boolean limitExtreme;
long maxKolors;
int maxPixels;
long iterations;
Point fractalSize; /* set to equal window size when drawn */
Point windowSize; /* set for fractal size; do not change fract size since still need it for zooming */
Point actualSize; /* actual size of fractal drawn */
} JS_fractal_120;
#define TRUE 1
#define FALSE 0
#define REMOVE_ALL_EVENTS -1
#define NIL_POINTER 0L
#define NIL_STRING "\p"
#define NAME_STRING "untitled"
#define IGNORED_STRING NIL_STRING
#define NIL_FILE_FILTER NIL_POINTER
#define NIL_DIALOG_HOOK NIL_POINTER
#define MOVE_TO_FRONT -1
#define DEFAULT_BUTTON 1
#define VERSION_DIALOG 400
#define VERSION_OK 1
#define VERSION_CANCEL 2
#define TOL 1.0e-5
#define VERSION 1.2
#define SYS_VERSION 2
JS_fractal_120 gFractal;
/*
FILES HAS BEEN EXTENSIVELY REWORKED (AGAIN).
This converts files from version 1.0x and 1.1x to version 1.2. This version
includes data on the version (so future versions only need to know the structure
number to read in data), type of file saved (currently 0 = preferences,
1 = mandelbrot, 2 = julia, and 3 = random walk; 0 and 3 are not written yet).
Information is also present on the fractal screen size and actual fractal
size).
This program simply shows a standard file dialog to get the file to convert.
If a file is "opened" or double-clicked, it will be converted and overwrite
the previous file. The dialog will then be shown again. Clicking on cancel quits
the program. Please note, this program will destroy data if version 1.2 is converted
to 1.2 (since both have the same creator and file type, version 1.2 files will be
displayed). This program assumes that those files that are double clicked
or opened are from version 1.0x or 1.1x.
*/
void main(void);
void ToolBoxInit(void);
void GetFileName( SFReply * );
void OpenBinaryFile( char * );
void ReadFileData( SFReply * );
void CurrDeviceSize( int *, int * );
void WriteFileData( SFReply * );
void DoVersionUpdate(void);
char HandleMightBeCurrVersion(void);
void centerWindow(WindowPtr);
void buttonProc (DialogPtr, int);
int NoSysError(void);
/* --------------------------------------------------------------------------------- */
void main(void)
{
char op = TRUE;
ToolBoxInit();
if (NoSysError())
{
while (op == TRUE)
{
OpenBinaryFile(&op);
}
}
ExitToShell();
}
/* --------------------------------------------------------------------------------- */
void ToolBoxInit(void)
{
InitGraf( &thePort );
InitFonts();
FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
InitWindows();
InitMenus();
TEInit();
InitDialogs( NIL_POINTER );
InitCursor();
}
/* --------------------------------------------------------------------------------- */
void GetFileName( replyPtr )
SFReply *replyPtr;
{
Point thePoint;
SFTypeList typeList;
int numTypes;
thePoint.h = 100;
thePoint.v = 100;
typeList[0] = 'fraJ';
numTypes = 1;
SFGetFile( thePoint, IGNORED_STRING, NIL_FILE_FILTER, numTypes,
&typeList, NIL_DIALOG_HOOK, replyPtr );
}
/* --------------------------------------------------------------------------------- */
void OpenBinaryFile( opened )
char *opened;
{
SFReply reply;
GetFileName( &reply );
if (reply.good)
{
ReadFileData( &reply );
DoVersionUpdate();
WriteFileData( &reply );
}
else
{
*opened = FALSE;
}
}
/* --------------------------------------------------------------------------------- */
void ReadFileData( replyPtr )
SFReply *replyPtr;
{
int srcFile;
OSErr theErr;
long siz;
int tempi;
long templ;
float tempf;
Boolean tempB;
char c = FALSE;
theErr = FSOpen( replyPtr->fName, replyPtr->vRefNum, &srcFile );
if (theErr == noErr)
{
SetFPos( srcFile, fsFromStart, 0L );
siz = (long) sizeof(float);
FSRead( srcFile, &siz, &tempf );
if ((fabs(tempf - TOL) < VERSION) && (fabs(tempf + TOL) > VERSION))
{
c = HandleMightBeCurrVersion();
}
if (c == TRUE)
{
FSClose( srcFile );
return;
}
SetFPos( srcFile, fsFromStart, 0L );
siz = (long) sizeof(int);
FSRead( srcFile, &siz, &(gFractal.function) );
siz = (long) sizeof(int);
FSRead( srcFile, &siz, &tempi );
siz = (long) sizeof(int);
FSRead( srcFile, &siz, &(gFractal.freedom) );
siz = (long) sizeof(double);
FSRead( srcFile, &siz, &(gFractal.centerX) );
siz = (long) sizeof(double);
FSRead( srcFile, &siz, &(gFractal.centerY) );
siz = (long) sizeof(double);
FSRead( srcFile, &siz, &(gFractal.scale) );
siz = (long) sizeof(double);
FSRead( srcFile, &siz, &(gFractal.fixedRe) );
siz = (long) sizeof(double);
FSRead( srcFile, &siz, &(gFractal.fixedIm) );
siz = (long) sizeof(Boolean);
FSRead( srcFile, &siz, &(gFractal.invertKolor) );
siz = (long) sizeof(Boolean);
FSRead( srcFile, &siz, &(gFractal.limitExtreme) );
siz = (long) sizeof(long);
FSRead( srcFile, &siz, &(gFractal.maxKolors) );
siz = (long) sizeof(Boolean);
FSRead( srcFile, &siz, &tempB );
siz = (long) sizeof(int);
FSRead( srcFile, &siz, &(gFractal.maxPixels) );
siz = (long) sizeof(long);
theErr = FSRead( srcFile, &siz, &templ );
if (theErr == noErr)
{
gFractal.accuracy = templ;
}
else
{
gFractal.accuracy = (long) tempi;
}
FSClose( srcFile );
}
}
/* --------------------------------------------------------------------------------- */
void CurrDeviceSize( x, y )
/* This returns the max screen size divided by 2 */
int *x;
int *y;
{
*x = (screenBits.bounds.right) / 2;
*y = (screenBits.bounds.bottom) / 2;
}
/* --------------------------------------------------------------------------------- */
void WriteFileData( replyPtr )
SFReply *replyPtr;
{
int srcFile;
OSErr theErr;
Str255 k;
long siz;
int i;
theErr = Create( replyPtr->fName, replyPtr->vRefNum, 'fraG', 'fraJ');
if ((theErr == noErr) || (theErr == dupFNErr))
{
theErr = FSOpen( replyPtr->fName, replyPtr->vRefNum, &srcFile );
if (theErr == noErr)
{
SetFPos( srcFile, fsFromStart, 0L );
siz = (long) sizeof(float);
FSWrite( srcFile, &siz, &(gFractal.version) );
siz = (long) sizeof(int);
FSWrite( srcFile, &siz, &(gFractal.function) );
siz = (long) sizeof(long);
FSWrite( srcFile, &siz, &(gFractal.accuracy) );
siz = (long) sizeof(int);
FSWrite( srcFile, &siz, &(gFractal.freedom) );
siz = (long) sizeof(double);
FSWrite( srcFile, &siz, &(gFractal.centerX) );
siz = (long) sizeof(double);
FSWrite( srcFile, &siz, &(gFractal.centerY) );
siz = (long) sizeof(double);
FSWrite( srcFile, &siz, &(gFractal.scale) );
siz = (long) sizeof(double);
FSWrite( srcFile, &siz, &(gFractal.fixedRe) );
siz = (long) sizeof(double);
FSWrite( srcFile, &siz, &(gFractal.fixedIm) );
siz = (long) sizeof(Boolean);
FSWrite( srcFile, &siz, &(gFractal.invertKolor) );
siz = (long) sizeof(Boolean);
FSWrite( srcFile, &siz, &(gFractal.limitExtreme) );
siz = (long) sizeof(long);
FSWrite( srcFile, &siz, &(gFractal.maxKolors) );
siz = (long) sizeof(int);
FSWrite( srcFile, &siz, &(gFractal.maxPixels) );
siz = (long) sizeof(long);
FSWrite( srcFile, &siz, &(gFractal.iterations) );
siz = (long) sizeof(int);
FSWrite( srcFile, &siz, &(gFractal.fractalSize.h) );
siz = (long) sizeof(int);
FSWrite( srcFile, &siz, &(gFractal.fractalSize.v) );
siz = (long) sizeof(int);
FSWrite( srcFile, &siz, &(gFractal.windowSize.h) );
siz = (long) sizeof(int);
FSWrite( srcFile, &siz, &(gFractal.windowSize.v) );
siz = (long) sizeof(int);
FSWrite( srcFile, &siz, &(gFractal.actualSize.h) );
siz = (long) sizeof(int);
FSWrite( srcFile, &siz, &(gFractal.actualSize.v) );
GetFPos( srcFile, &siz );
SetEOF( srcFile, siz );
FSClose( srcFile );
FlushVol(0L, replyPtr->vRefNum);
}
}
}
/* --------------------------------------------------------------------------------- */
void DoVersionUpdate(void)
{
int x;
int y;
gFractal.version = VERSION;
CurrDeviceSize(&x,&y);
gFractal.windowSize.h = x * 2;
gFractal.windowSize.v = y * 2;
gFractal.fractalSize.h = 0;
gFractal.fractalSize.v = 0;
gFractal.actualSize.h = 0;
gFractal.actualSize.v = 0;
}
/* --------------------------------------------------------------------------------- */
char HandleMightBeCurrVersion(void)
{
Boolean dialogDone = FALSE;
int itemHit;
int itemType;
Rect itemRect;
Handle itemHandle;
char c;
DialogPtr gSettingsDialog;
gSettingsDialog = GetNewDialog( VERSION_DIALOG, NIL_POINTER, MOVE_TO_FRONT );
centerWindow( gSettingsDialog );
ShowWindow( gSettingsDialog );
buttonProc( gSettingsDialog, DEFAULT_BUTTON );
while (dialogDone == FALSE)
{
ModalDialog( NIL_POINTER, &itemHit );
switch( itemHit )
{
case VERSION_OK:
c = FALSE;
dialogDone = TRUE;
break;
case VERSION_CANCEL:
c = TRUE;
dialogDone = TRUE;
break;
default:
break;
}
}
HideWindow( gSettingsDialog );
DisposDialog( gSettingsDialog );
return c;
}
/* --------------------------------------------------------------------------------- */
void centerWindow(theWindow)
WindowPtr theWindow;
{
short scrLength, scrHeight;
Rect globalRect;
Point where;
/* get the window rect in global coordinates */
CurrDeviceSize( &scrLength, &scrHeight );
globalRect = (*theWindow).portRect;
scrLength *= 2;
scrHeight *= 2;
where.v = (scrHeight - (globalRect.bottom - globalRect.top)) / 2;
where.h = (scrLength - (globalRect.right - globalRect.left)) / 2 + GetMBarHeight();
MoveWindow (theWindow, where.h, where.v, FALSE);
} /* centerWindow */
/* --------------------------------------------------------------------------------- */
void buttonProc (theDialog, theItem)
DialogPtr theDialog;
int theItem;
{
short type;
Rect box;
Handle itemHdl;
GrafPtr oldGrafPtr;
GetPort( &oldGrafPtr );
SetPort(theDialog);
GetDItem (theDialog, theItem, &type, &itemHdl, &box);
PenSize (3, 3);
InsetRect (&box, -4, -4);
FrameRoundRect (&box, 16, 16);
PenNormal ();
SetPort( oldGrafPtr );
} /* buttonProc */
/* --------------------------------------------------------------------------------- */
int NoSysError(void)
{
OSErr status;
SysEnvRec SysEnvData;
status = SysEnvirons( SYS_VERSION, &SysEnvData );
if (( status != noErr ) || ( SysEnvData.processor < env68020 ) ||
( SysEnvData.systemVersion < 0x0600 ) || ( SysEnvData.hasFPU != TRUE ) ||
( SysEnvData.hasColorQD != TRUE ))
{
return(FALSE);
}
else
{
return(TRUE);
}
}